package com.stylefeng.guns.common.constant.state;

import org.apache.commons.collections.map.ListOrderedMap;

import java.lang.reflect.Method;
import java.util.*;

public interface EnumCommon {
    /**
     * 根据反射，通过方法名称获取方法值，忽略大小写的
     *
     * @param methodName
     * @param obj
     * @param args
     * @return return key
     */
    static <T> Object getMethodKey(String methodName, T obj, Object... args) {
        Object resut = "";
        try {
            Method[] methods = obj.getClass().getMethods();
            if (methods.length <= 0) {
                return resut;
            }
            Method method = null;
            for (int i = 0, len = methods.length; i < len; i++) {
                if (methods[i].getName().equalsIgnoreCase(methodName)) {
                    method = methods[i];
                    break;
                }
            }

            if (method == null) {
                return resut;
            }
            resut = method.invoke(obj, args);
            if (resut == null) {
                resut = "";
            }
            return resut;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resut;
    }

    /**
     * 通过key值获取对应的描述信息
     *
     * @param enumCommon
     * @param key
     * @return enum description
     */
    static <T> String valueByKey(Class<T> enumCommon, Object key) {
        if (!enumCommon.isEnum()) {
            return "";
        }
        T[] enums = enumCommon.getEnumConstants();
        if (enums == null || enums.length <= 0) {
            return "";
        }
        String keyMathod = "getKey";
        String valueMathod = "getValue";
        for (int i = 0, len = enums.length; i < len; i++) {
            T each = enums[i];
            try {
                Object resultKey = getMethodKey(keyMathod, each);
                if (resultKey.toString().equals(key + "")) {
                    Object resultValue = getMethodKey(valueMathod, each);
                    return String.valueOf(resultValue);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /**
     * 枚举转map结合key作为map的key,description作为map的key
     *
     * @param enumCommon
     * @return enum mapcolloction
     */
    static <T> Map<Object, String> toMap(Class<T> enumCommon) {
        Map<Object, String> map = new ListOrderedMap();
        if (!enumCommon.isEnum()) {
            return map;
        }
        T[] enums = enumCommon.getEnumConstants();
        if (enums == null || enums.length <= 0) {
            return map;
        }
        String keyMathod = "getKey";
        String valueMathod = "getValue";
        for (int i = 0, len = enums.length; i < len; i++) {
            T tobj = enums[i];
            try {
                Object resultKey = getMethodKey(keyMathod, tobj);
                if ("".equals(resultKey)) {
                    continue;
                }
                Object resultValue = getMethodKey(valueMathod, tobj);
                if ("".equals(resultValue)) {
                    resultValue = tobj;
                }
                map.put(resultKey, resultValue + "");

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    Object getKey();

    String getValue();
}